<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\xs\model;

use think\Model;
use think\db\Query;

class XmglZzjgModel extends Model
{

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'xmgl_zzjg';

    /**
     * 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
     * @var bool|string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 组织机构查询
     * @param       $filter
     * @param       $field
     * @param       $pageNumber
     * @param       $pageSize
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function searchList($filter, $field = "*", $pageNumber = 1, $pageSize = 20)
    {

        $where = function (Query $query) use ($filter) {
            $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
            if (!empty($keyword)) {
                $query->whereOr('zzjg_mc', 'like', "%$keyword%");
            }

            $zzjg_id = empty($filter['zzjg_id']) ? '' : $filter['zzjg_id'];
            if (!empty($zzjg_id)) {
                $query->whereOr('id', '=', $zzjg_id);
            }
        };

        $where1 = [
            ['create_time','>=',0],
            ['delete_time','=',0]];

        $order = ['list_order' => 'ASC'];

        $total = $this
            ->where($where1)
            ->where($where)
            ->count();

        $res = $this->field($field)
            ->where($where1)
            ->where($where)
            ->order($order)
            ->limit($pageSize)
            ->page($pageNumber)
            ->select();

        /** ->fetchSql(true) ，查询参数：?keyword=湖&zzjg_id=3，生成sql：
         * "SELECT * FROM `xmgl_zzjg` WHERE  
         *  `create_time` >= 0  AND `delete_time` = 0  AND
         * (  `zzjg_mc` LIKE '%湖%'  OR `id` = 3 ) 
         * ORDER BY `list_order` DESC LIMIT 0,20"
         */
        $sqlstr = $this->field($field)
            ->where($where1)
            ->where($where)
            ->order($order)
            ->limit($pageSize)
            ->page($pageNumber)
            ->fetchSql(true) 
            ->select();

        $d = [
            'pageSize'          => $pageSize,
            'pageNumber'        => $pageNumber,
            "totalPage"         => ceil($total / $pageSize),
            'total'             => $total,
            'rows'              => $res,
            'where' => $sqlstr,
        ];
        return $d;
    }
}
